<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
	<title>API: json   JSON.js  (YUI Library)</title>
	<link rel="stylesheet" type="text/css" href="assets/api.css">
    <script type="text/javascript" src="assets/api-js"></script>
    <script type="text/javascript" src="assets/ac-js"></script>
</head>

<body id="yahoo-com">

<div id="doc3" class="yui-t2">

	<div id="hd">
        <a href="http://developer.yahoo.com/yui/"><h1>Yahoo! UI Library</h1></a>
        <h3>json&nbsp; <span class="subtitle">2.4.1</span></h3>
        <p>
        <a href="./index.html">Yahoo! UI Library</a> 
            &gt; <a href="./module_json.html">json</a>
                
                 &gt; JSON.js (source view) 
            </p>

	</div>

	<div id="bd">
		<div id="yui-main">
			<div class="yui-b">
            <form name="yui-classopts-form">
    <span id="classopts"><input type="checkbox" name="showprivate" id="showprivate" /> Show Private</span>
    <span id="classopts"><input type="checkbox" name="showprotected" id="showprotected" /> Show Protected</span>
            </form>

                    <div id="srcout">
                        <style>
                            #doc3 #classopts { display:none; }
                        </style>
<div class="highlight" ><pre><span class="c">/**</span>
<span class="c"> * Provides methods to parse JSON strings and convert objects to JSON strings.</span>
<span class="c"> * @module json</span>
<span class="c"> * @requires yahoo</span>
<span class="c"> * @class YAHOO.lang.JSON</span>
<span class="c"> * @static</span>
<span class="c"> */</span>
<span class="nx">YAHOO</span><span class="o">.</span><span class="nx">namespace</span><span class="o">(</span><span class="s1">&#39;lang&#39;</span><span class="o">);</span>
<span class="nx">YAHOO</span><span class="o">.</span><span class="nx">lang</span><span class="o">.</span><span class="nx">JSON</span> <span class="o">=</span> <span class="o">{</span>

    <span class="c">/**</span>
<span class="c">     * Parse a JSON string, returning the native JavaScript representation.</span>
<span class="c">     * Only minor modifications from http://www.json.org/json.js.</span>
<span class="c">     * @param s {string} JSON string data</span>
<span class="c">     * @param filter {function} (optional) function(k,v) passed each key value pair of object literals, allowing pruning or altering values</span>
<span class="c">     * @return {MIXED} the native JavaScript representation of the JSON string</span>
<span class="c">     * @throws SyntaxError</span>
<span class="c">     * @method parse</span>
<span class="c">     * @static</span>
<span class="c">     * @public</span>
<span class="c">     */</span>
    <span class="nx">parse</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">s</span><span class="o">,</span><span class="nx">filter</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">var</span> <span class="nx">j</span><span class="o">;</span>

        <span class="k">function</span> <span class="nx">walk</span><span class="o">(</span><span class="nx">k</span><span class="o">,</span> <span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">var</span> <span class="nx">i</span><span class="o">,</span> <span class="nx">n</span><span class="o">;</span>
            <span class="k">if</span> <span class="o">(</span><span class="nx">v</span> <span class="o">&amp;&amp;</span> <span class="k">typeof</span> <span class="nx">v</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="k">in</span> <span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nx">YAHOO</span><span class="o">.</span><span class="nx">lang</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">.</span><span class="nx">apply</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span> <span class="o">[</span><span class="nx">i</span><span class="o">]))</span> <span class="o">{</span>
                        <span class="nx">n</span> <span class="o">=</span> <span class="nx">walk</span><span class="o">(</span><span class="nx">i</span><span class="o">,</span> <span class="nx">v</span><span class="o">[</span><span class="nx">i</span><span class="o">]);</span>
                        <span class="k">if</span> <span class="o">(</span><span class="nx">n</span> <span class="o">!==</span> <span class="kc">undefined</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nx">v</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="nx">n</span><span class="o">;</span>
                        <span class="o">}</span>
                    <span class="o">}</span>
                <span class="o">}</span>
            <span class="o">}</span>
            <span class="k">return</span> <span class="nx">filter</span><span class="o">(</span><span class="nx">k</span><span class="o">,</span> <span class="nx">v</span><span class="o">);</span>
        <span class="o">}</span>


<span class="c">// Parsing happens in three stages. In the first stage, we run the text against</span>
<span class="c">// a regular expression which looks for non-JSON characters. We are especially</span>
<span class="c">// concerned with &#39;()&#39; and &#39;new&#39; because they can cause invocation, and &#39;=&#39;</span>
<span class="c">// because it can cause mutation. But just to be safe, we will reject all</span>
<span class="c">// unexpected characters.</span>
<span class="c"></span>
<span class="c">// We split the first stage into 4 regexp operations in order to work around</span>
<span class="c">// crippling deficiencies in IE&#39;s and Safari&#39;s regexp engines. First we replace</span>
<span class="c">// all backslash pairs with &#39;@&#39; (a non-JSON character). Second, we replace all</span>
<span class="c">// simple value tokens with &#39;]&#39; characters. Third, we delete all open brackets</span>
<span class="c">// that follow a colon or comma or that begin the text. Finally, we look to see</span>
<span class="c">// that the remaining characters are only whitespace or &#39;]&#39; or &#39;,&#39; or &#39;:&#39; or &#39;{&#39;</span>
<span class="c">// or &#39;}&#39;. If that is so, then the text is safe for eval.</span>
<span class="c"></span>
        <span class="k">if</span> <span class="o">(</span><span class="sr">/^[\],:{}\s]*$/</span><span class="o">.</span><span class="nx">test</span><span class="o">(</span><span class="nx">s</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="sr">/\\./g</span><span class="o">,</span> <span class="s1">&#39;@&#39;</span><span class="o">).</span>
                <span class="nx">replace</span><span class="o">(</span><span class="sr">/&quot;[^&quot;\\\n\r]*&quot;|true|false|null|-?\d+(?:\.\d*)?(:?[eE][+\-]?\d+)?/g</span><span class="o">,</span> <span class="s1">&#39;]&#39;</span><span class="o">).</span>
                <span class="nx">replace</span><span class="o">(</span><span class="sr">/(?:^|:|,)(?:\s*\[)+/g</span><span class="o">,</span> <span class="s1">&#39;&#39;</span><span class="o">)))</span> <span class="o">{</span>

<span class="c">// In the second stage we use the eval function to compile the text into a</span>
<span class="c">// JavaScript structure. The &#39;{&#39; operator is subject to a syntactic ambiguity</span>
<span class="c">// in JavaScript: it can begin a block or an object literal. We wrap the text</span>
<span class="c">// in parens to eliminate the ambiguity.</span>
<span class="c"></span>
            <span class="nx">j</span> <span class="o">=</span> <span class="nb">eval</span><span class="o">(</span><span class="s1">&#39;(&#39;</span> <span class="o">+</span> <span class="nx">s</span> <span class="o">+</span> <span class="s1">&#39;)&#39;</span><span class="o">);</span>

<span class="c">// In the optional third stage, we recursively walk the new structure, passing</span>
<span class="c">// each name/value pair to a filter function for possible transformation.</span>
<span class="c"></span>
            <span class="k">return</span> <span class="k">typeof</span> <span class="nx">filter</span> <span class="o">===</span> <span class="s1">&#39;function&#39;</span> <span class="o">?</span> <span class="nx">walk</span><span class="o">(</span><span class="s1">&#39;&#39;</span><span class="o">,</span> <span class="nx">j</span><span class="o">)</span> <span class="o">:</span> <span class="nx">j</span><span class="o">;</span>
        <span class="o">}</span>

<span class="c">// If the text is not JSON parseable, then a SyntaxError is thrown.</span>
<span class="c"></span>
        <span class="k">throw</span> <span class="k">new</span> <span class="nx">SyntaxError</span><span class="o">(</span><span class="s1">&#39;parseJSON&#39;</span><span class="o">);</span>
    <span class="o">},</span>


    <span class="c">/**</span>
<span class="c">     * Converts an arbitrary value to a JSON string representation.</span>
<span class="c">     * Cyclical object or array references are replaced with null.</span>
<span class="c">     * If a whitelist is provided, only matching object keys will be included.</span>
<span class="c">     * If a depth limit is provided, objects and arrays at that depth will</span>
<span class="c">     * be stringified as empty.</span>
<span class="c">     * @param o {MIXED} any arbitrary object to convert to JSON string</span>
<span class="c">     * @param w {Array} (optional) whitelist of acceptable object keys to include</span>
<span class="c">     * @param d {number} (optional) depth limit to recurse objects/arrays (practical minimum 1)</span>
<span class="c">     * @return {string} JSON string representation of the input</span>
<span class="c">     * @method stringify</span>
<span class="c">     * @static</span>
<span class="c">     * @public</span>
<span class="c">     */</span>
    <span class="nx">stringify</span> <span class="o">:</span> <span class="k">function</span> <span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">)</span> <span class="o">{</span>

        <span class="k">var</span> <span class="nx">l</span> <span class="o">=</span> <span class="nx">YAHOO</span><span class="o">.</span><span class="nx">lang</span><span class="o">,</span>

            <span class="c">// Regex used to encode strings as safe JSON values</span>
<span class="c"></span>            <span class="nx">str_re</span> <span class="o">=</span> <span class="sr">/[&quot;\\\x00-\x1f]/g</span><span class="o">,</span>

            <span class="c">// Character substitution map used by regex to prepare strings</span>
<span class="c"></span>            <span class="nx">m</span> <span class="o">=</span> <span class="o">{</span>
                <span class="s1">&#39;\b&#39;</span><span class="o">:</span> <span class="s1">&#39;\\b&#39;</span><span class="o">,</span>
                <span class="s1">&#39;\t&#39;</span><span class="o">:</span> <span class="s1">&#39;\\t&#39;</span><span class="o">,</span>
                <span class="s1">&#39;\n&#39;</span><span class="o">:</span> <span class="s1">&#39;\\n&#39;</span><span class="o">,</span>
                <span class="s1">&#39;\f&#39;</span><span class="o">:</span> <span class="s1">&#39;\\f&#39;</span><span class="o">,</span>
                <span class="s1">&#39;\r&#39;</span><span class="o">:</span> <span class="s1">&#39;\\r&#39;</span><span class="o">,</span>
                <span class="s1">&#39;&quot;&#39;</span> <span class="o">:</span> <span class="s1">&#39;\\&quot;&#39;</span><span class="o">,</span>
                <span class="s1">&#39;\\&#39;</span><span class="o">:</span> <span class="s1">&#39;\\\\&#39;</span>
            <span class="o">},</span>

            <span class="c">// Processing stack used to prevent cyclical references</span>
<span class="c"></span>            <span class="nx">pstack</span>  <span class="o">=</span> <span class="o">[];</span>


        <span class="c">/**</span>
<span class="c">        * Encode odd characters.  Translated characters are cached.</span>
<span class="c">        * @private</span>
<span class="c">        */</span>
        <span class="k">function</span> <span class="nx">_encodeChar</span><span class="o">(</span><span class="nx">c</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">if</span> <span class="o">(!</span><span class="nx">m</span><span class="o">[</span><span class="nx">c</span><span class="o">])</span> <span class="o">{</span>
                <span class="k">var</span> <span class="nx">a</span> <span class="o">=</span> <span class="nx">c</span><span class="o">.</span><span class="nx">charCodeAt</span><span class="o">();</span>
                <span class="nx">m</span><span class="o">[</span><span class="nx">c</span><span class="o">]</span> <span class="o">=</span> <span class="s1">&#39;\\u00&#39;</span> <span class="o">+</span> <span class="nb">Math</span><span class="o">.</span><span class="nx">floor</span><span class="o">(</span><span class="nx">a</span> <span class="o">/</span> <span class="m">16</span><span class="o">).</span><span class="nx">toString</span><span class="o">(</span><span class="m">16</span><span class="o">)</span> <span class="o">+</span>
                                           <span class="o">(</span><span class="nx">a</span> <span class="o">%</span> <span class="m">16</span><span class="o">).</span><span class="nx">toString</span><span class="o">(</span><span class="m">16</span><span class="o">);</span>
            <span class="o">}</span>
            <span class="k">return</span> <span class="nx">m</span><span class="o">[</span><span class="nx">c</span><span class="o">];</span>
        <span class="o">}</span>

        <span class="c">/**</span>
<span class="c">         * zero pad single digits in dates.</span>
<span class="c">         * @private</span>
<span class="c">         */</span>
        <span class="k">function</span> <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">v</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="nx">v</span> <span class="o">&lt;</span> <span class="m">10</span> <span class="o">?</span> <span class="s1">&#39;0&#39;</span> <span class="o">+</span> <span class="nx">v</span> <span class="o">:</span> <span class="nx">v</span><span class="o">;</span>
        <span class="o">}</span>

        <span class="c">/**</span>
<span class="c">         * Wrap string values and object keys in double quotes after replacing</span>
<span class="c">         * any odd characters.</span>
<span class="c">         * @private</span>
<span class="c">         */</span>
        <span class="k">function</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">return</span> <span class="s1">&#39;&quot;&#39;</span> <span class="o">+</span> <span class="nx">o</span><span class="o">.</span><span class="nx">replace</span><span class="o">(</span><span class="nx">str_re</span><span class="o">,</span> <span class="nx">_encodeChar</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;&quot;&#39;</span><span class="o">;</span>
        <span class="o">}</span>
    
        <span class="c">/**</span>
<span class="c">         * Worker function.  Fork behavior on data type and recurse objects and</span>
<span class="c">         * arrays per the configured depth.</span>
<span class="c">         * @private</span>
<span class="c">         */</span>
        <span class="k">function</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">)</span> <span class="o">{</span>
            <span class="k">var</span> <span class="nx">t</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">o</span><span class="o">,</span>
                <span class="nx">i</span><span class="o">,</span><span class="nx">len</span><span class="o">,</span><span class="nx">j</span><span class="o">,</span> <span class="c">// array iteration</span>
<span class="c"></span>                <span class="nx">k</span><span class="o">,</span><span class="nx">v</span><span class="o">,</span>     <span class="c">// object iteration</span>
<span class="c"></span>                <span class="nx">vt</span><span class="o">,</span>      <span class="c">// typeof v during iteration</span>
<span class="c"></span>                <span class="nx">a</span><span class="o">;</span>       <span class="c">// composition array for performance over string concat</span>
<span class="c"></span>
            <span class="c">// String</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">t</span> <span class="o">===</span> <span class="s1">&#39;string&#39;</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
            <span class="o">}</span>

            <span class="c">// native boolean and Boolean instance</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">t</span> <span class="o">===</span> <span class="s1">&#39;boolean&#39;</span> <span class="o">||</span> <span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">Boolean</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="nb">String</span><span class="o">(</span><span class="nx">o</span><span class="o">);</span>
            <span class="o">}</span>

            <span class="c">// native number and Number instance</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">t</span> <span class="o">===</span> <span class="s1">&#39;number&#39;</span> <span class="o">||</span> <span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">Number</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="nb">isFinite</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">?</span> <span class="nb">String</span><span class="o">(</span><span class="nx">o</span><span class="o">)</span> <span class="o">:</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="c">// Date</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">o</span> <span class="k">instanceof</span> <span class="nb">Date</span><span class="o">)</span> <span class="o">{</span>
                <span class="k">return</span> <span class="o">[</span><span class="s1">&#39;&quot;&#39;</span><span class="o">,</span>         <span class="nx">o</span><span class="o">.</span><span class="nx">getUTCFullYear</span><span class="o">(),</span>  <span class="s1">&#39;-&#39;</span><span class="o">,</span>
                            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">getUTCMonth</span><span class="o">()</span> <span class="o">+</span> <span class="m">1</span><span class="o">),</span><span class="s1">&#39;-&#39;</span><span class="o">,</span>
                            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">getUTCDate</span><span class="o">()),</span>     <span class="s1">&#39;T&#39;</span><span class="o">,</span>
                            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">getUTCHours</span><span class="o">()),</span>    <span class="s1">&#39;:&#39;</span><span class="o">,</span>
                            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">getUTCMinutes</span><span class="o">()),</span>  <span class="s1">&#39;:&#39;</span><span class="o">,</span>
                            <span class="nx">_zeroPad</span><span class="o">(</span><span class="nx">o</span><span class="o">.</span><span class="nx">getUTCSeconds</span><span class="o">()),</span>  <span class="s1">&#39;Z&quot;&#39;</span><span class="o">].</span><span class="nx">join</span><span class="o">(</span><span class="s1">&#39;&#39;</span><span class="o">);</span>
            <span class="o">}</span>

            <span class="c">// Array</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">l</span><span class="o">.</span><span class="nx">isArray</span><span class="o">(</span><span class="nx">o</span><span class="o">))</span> <span class="o">{</span>
                <span class="c">// Check for cyclical references</span>
<span class="c"></span>                <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">pstack</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nx">pstack</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
                    <span class="o">}</span>
                <span class="o">}</span>

                <span class="c">// Add the array to the processing stack</span>
<span class="c"></span>                <span class="nx">pstack</span><span class="o">[</span><span class="nx">pstack</span><span class="o">.</span><span class="nx">length</span><span class="o">]</span> <span class="o">=</span> <span class="nx">o</span><span class="o">;</span>

                <span class="nx">a</span> <span class="o">=</span> <span class="o">[];</span>
                <span class="c">// Only recurse if we&#39;re above depth config</span>
<span class="c"></span>                <span class="k">if</span> <span class="o">(</span><span class="nx">d</span> <span class="o">&gt;</span> <span class="m">0</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">o</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                        <span class="nx">a</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">=</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">o</span><span class="o">[</span><span class="nx">i</span><span class="o">],</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">-</span><span class="m">1</span><span class="o">);</span>
                    <span class="o">}</span>
                <span class="o">}</span>

                <span class="c">// remove the array from the stack</span>
<span class="c"></span>                <span class="nx">pstack</span><span class="o">.</span><span class="nx">pop</span><span class="o">();</span>

                <span class="k">return</span> <span class="s1">&#39;[&#39;</span> <span class="o">+</span> <span class="nx">a</span><span class="o">.</span><span class="nx">join</span><span class="o">(</span><span class="s1">&#39;,&#39;</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;]&#39;</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="c">// Object</span>
<span class="c"></span>            <span class="k">if</span> <span class="o">(</span><span class="nx">t</span> <span class="o">===</span> <span class="s1">&#39;object&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
                <span class="c">// Check for cyclical references</span>
<span class="c"></span>                <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">pstack</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                    <span class="k">if</span> <span class="o">(</span><span class="nx">pstack</span><span class="o">[</span><span class="nx">i</span><span class="o">]</span> <span class="o">===</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
                        <span class="k">return</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
                    <span class="o">}</span>
                <span class="o">}</span>

                <span class="c">// Add the object to the  processing stack</span>
<span class="c"></span>                <span class="nx">pstack</span><span class="o">[</span><span class="nx">pstack</span><span class="o">.</span><span class="nx">length</span><span class="o">]</span> <span class="o">=</span> <span class="nx">o</span><span class="o">;</span>

                <span class="nx">a</span> <span class="o">=</span> <span class="o">[];</span>
                <span class="c">// Only recurse if we&#39;re above depth config</span>
<span class="c"></span>                <span class="k">if</span> <span class="o">(</span><span class="nx">d</span> <span class="o">&gt;</span> <span class="m">0</span><span class="o">)</span> <span class="o">{</span>

                    <span class="c">// If whitelist provided, take only those keys</span>
<span class="c"></span>                    <span class="k">if</span> <span class="o">(</span><span class="nx">w</span><span class="o">)</span> <span class="o">{</span>
                        <span class="k">for</span> <span class="o">(</span><span class="nx">i</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">j</span> <span class="o">=</span> <span class="m">0</span><span class="o">,</span> <span class="nx">len</span> <span class="o">=</span> <span class="nx">w</span><span class="o">.</span><span class="nx">length</span><span class="o">;</span> <span class="nx">i</span> <span class="o">&lt;</span> <span class="nx">len</span><span class="o">;</span> <span class="o">++</span><span class="nx">i</span><span class="o">)</span> <span class="o">{</span>
                            <span class="nx">v</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">w</span><span class="o">[</span><span class="nx">i</span><span class="o">]];</span>
                            <span class="nx">vt</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">v</span><span class="o">;</span>

                            <span class="c">// Omit invalid values</span>
<span class="c"></span>                            <span class="k">if</span> <span class="o">(</span><span class="nx">vt</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">vt</span> <span class="o">!==</span> <span class="s1">&#39;function&#39;</span><span class="o">)</span> <span class="o">{</span>
                                <span class="nx">a</span><span class="o">[</span><span class="nx">j</span><span class="o">++]</span> <span class="o">=</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">w</span><span class="o">[</span><span class="nx">i</span><span class="o">])</span> <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">-</span><span class="m">1</span><span class="o">);</span>
                            <span class="o">}</span>
                        <span class="o">}</span>

                    <span class="c">// Otherwise, take all valid object properties</span>
<span class="c"></span>                    <span class="c">// omitting the prototype chain properties</span>
<span class="c"></span>                    <span class="o">}</span> <span class="k">else</span> <span class="o">{</span>
                        <span class="nx">j</span> <span class="o">=</span> <span class="m">0</span><span class="o">;</span>
                        <span class="k">for</span> <span class="o">(</span><span class="nx">k</span> <span class="k">in</span> <span class="nx">o</span><span class="o">)</span> <span class="o">{</span>
                            <span class="k">if</span> <span class="o">(</span><span class="k">typeof</span> <span class="nx">k</span> <span class="o">===</span> <span class="s1">&#39;string&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">l</span><span class="o">.</span><span class="nx">hasOwnProperty</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">k</span><span class="o">))</span> <span class="o">{</span>
                                <span class="nx">v</span> <span class="o">=</span> <span class="nx">o</span><span class="o">[</span><span class="nx">k</span><span class="o">];</span>
                                <span class="nx">vt</span> <span class="o">=</span> <span class="k">typeof</span> <span class="nx">v</span><span class="o">;</span>
                                <span class="k">if</span> <span class="o">(</span><span class="nx">vt</span> <span class="o">!==</span> <span class="s1">&#39;undefined&#39;</span> <span class="o">&amp;&amp;</span> <span class="nx">vt</span> <span class="o">!==</span> <span class="s1">&#39;function&#39;</span><span class="o">)</span> <span class="o">{</span>
                                    <span class="nx">a</span><span class="o">[</span><span class="nx">j</span><span class="o">++]</span> <span class="o">=</span> <span class="nx">_string</span><span class="o">(</span><span class="nx">k</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;:&#39;</span> <span class="o">+</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">v</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">-</span><span class="m">1</span><span class="o">);</span>
                                <span class="o">}</span>
                            <span class="o">}</span>
                        <span class="o">}</span>
                    <span class="o">}</span>
                <span class="o">}</span>

                <span class="c">// Remove the object from processing stack</span>
<span class="c"></span>                <span class="nx">pstack</span><span class="o">.</span><span class="nx">pop</span><span class="o">();</span>

                <span class="k">return</span> <span class="s1">&#39;{&#39;</span> <span class="o">+</span> <span class="nx">a</span><span class="o">.</span><span class="nx">join</span><span class="o">(</span><span class="s1">&#39;,&#39;</span><span class="o">)</span> <span class="o">+</span> <span class="s1">&#39;}&#39;</span><span class="o">;</span>
            <span class="o">}</span>

            <span class="k">return</span> <span class="s1">&#39;null&#39;</span><span class="o">;</span>
        <span class="o">}</span>

        <span class="c">// process the input</span>
<span class="c"></span>        <span class="nx">d</span> <span class="o">=</span> <span class="nx">d</span> <span class="o">&gt;=</span> <span class="m">0</span> <span class="o">?</span> <span class="nx">d</span> <span class="o">:</span> <span class="m">1</span><span class="sr">/0;  /</span><span class="o">/</span> <span class="nx">Default</span> <span class="nx">depth</span> <span class="nx">to</span> <span class="nx">POSITIVE_INFINITY</span>
        <span class="k">return</span> <span class="nx">_stringify</span><span class="o">(</span><span class="nx">o</span><span class="o">,</span><span class="nx">w</span><span class="o">,</span><span class="nx">d</span><span class="o">);</span>
    <span class="o">}</span>
<span class="o">};</span>
</pre></div>
                    </div>
			</div>
		</div>
		<div class="yui-b">
            <div class="nav">

                    <div class="module">
                        <h4>Modules</h4>
                        <ul class="content">

                                <li class=""><a href="module_animation.html">animation</a></li>

                                <li class=""><a href="module_autocomplete.html">autocomplete</a></li>

                                <li class=""><a href="module_button.html">button</a></li>

                                <li class=""><a href="module_calendar.html">calendar</a></li>

                                <li class=""><a href="module_charts.html">charts</a></li>

                                <li class=""><a href="module_colorpicker.html">colorpicker</a></li>

                                <li class=""><a href="module_connection.html">connection</a></li>

                                <li class=""><a href="module_container.html">container</a></li>

                                <li class=""><a href="module_datasource.html">datasource</a></li>

                                <li class=""><a href="module_datatable.html">datatable</a></li>

                                <li class=""><a href="module_date.html">date</a></li>

                                <li class=""><a href="module_dom.html">dom</a></li>

                                <li class=""><a href="module_dragdrop.html">dragdrop</a></li>

                                <li class=""><a href="module_editor.html">editor</a></li>

                                <li class=""><a href="module_element.html">element</a></li>

                                <li class=""><a href="module_event.html">event</a></li>

                                <li class=""><a href="module_get.html">get</a></li>

                                <li class=""><a href="module_history.html">history</a></li>

                                <li class=""><a href="module_imageloader.html">imageloader</a></li>

                                <li class="selected"><a href="module_json.html">json</a></li>

                                <li class=""><a href="module_logger.html">logger</a></li>

                                <li class=""><a href="module_menu.html">menu</a></li>

                                <li class=""><a href="module_number.html">number</a></li>

                                <li class=""><a href="module_profiler.html">profiler</a></li>

                                <li class=""><a href="module_selector.html">selector</a></li>

                                <li class=""><a href="module_slider.html">slider</a></li>

                                <li class=""><a href="module_tabview.html">tabview</a></li>

                                <li class=""><a href="module_treeview.html">treeview</a></li>

                                <li class=""><a href="module_yahoo.html">yahoo</a></li>

                                <li class=""><a href="module_yuiloader.html">yuiloader</a></li>

                                <li class=""><a href="module_yuitest.html">yuitest</a></li>
                        </ul>
                    </div>

                    <div class="module">
                        <h4>Classes</h4>
                        <ul class="content">
                                <li class=""><a href="YAHOO.lang.JSON.html">YAHOO.lang.JSON</a></li>
                        </ul>
                    </div>

                    <div class="module">
                        <h4>Files</h4>
                        <ul class="content">        
                                <li class="selected"><a href="JSON.js.html">JSON.js</a></li>
                        </ul>
                    </div>





            </div>
		</div>
	</div>
	<div id="ft">
        <hr />
        Copyright &copy; 2007 Yahoo! Inc. All rights reserved.
	</div>
</div>
</body>
</html>
